#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
  ui->setupUi(this);
  this->setWindowTitle("UDP服务端");
  qDebug() << "main 线程ID=" << QThread::currentThreadId();

}

Widget::~Widget() { delete ui; }

void Widget::on_btnStart_clicked() {
  if (!m_start) {
      QString ip = ui->lineEditIP->text();
      uint port = ui->lineEditPort->text().toUInt();
      m_workThread.reset(new WorkThread(QHostAddress(ip), port));
      m_workThread->start();
      ui->btnStart->setText("停止");
      connect(m_workThread.get(), &WorkThread::showMessage, this ,&Widget::showMessage,Qt::ConnectionType::UniqueConnection);
  } else {
    // 关闭TCP的监听
      m_workThread->stopThread();
      showMessage("服务器已停止!");
      ui->btnStart->setText("启动");
      m_start = false;
  }
}

void Widget::showMessage(const QString &info) {
  ui->messageShoxEdit->appendPlainText(info);
}

void Widget::on_btnSend_clicked() {

  // 获取要发送的内容
  QString msg = ui->messageEdit->text();
  QByteArray data = msg.toUtf8(); // 转换为utf8字节数组
  m_workThread->sendData(data);


}
